Micron Document




Randomized algorithm
part 3/32 · 53.0 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Motivation

As a motivating example, consider the problem of finding an ‘a’ in an array of n elements.

Input: An array of n≥2 elements, in which half are ‘a’s and the other half are ‘b’s.

Output: Find an ‘a’ in the array.

We give two versions of the algorithm, one Las Vegas algorithm and one Monte Carlo algorithm.

Las Vegas algorithm:

findingA_LV(array A, n)
begin
repeat
Randomly select one element out of n elements.
until 'a' is found
end

This algorithm succeeds with probability 1. The number of iterations varies and can be arbitrarily large, but the expected number of iterations is

lim n → → ∞ ∞ ∑ ∑ i = 1 n i 2 i = 2 {\displaystyle \lim _{n\to \infty }\sum _{i=1}^{n}{\frac {i}{2^{i}}}=2}

Since it is constant, the expected run time over many calls is Θ Θ ( 1 ) {\displaystyle \Theta (1)} . (See Big Theta notation)

Monte Carlo algorithm:

findingA_MC(array A, n, k)
begin
i := 0
repeat
Randomly select one element out of n elements.
i := i + 1
until i = k or 'a' is found
end

If an ‘a’ is found, the algorithm succeeds, else the algorithm fails. After k iterations, the probability of finding an ‘a’ is:

Pr [ f i n d a ] = 1 − − ( 1 / 2 ) k {\displaystyle \Pr[\mathrm {find~a} ]=1-(1/2)^{k}}

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────